package cc.iotkit.common.excel.convert;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.text.SimpleDateFormat;
import java.util.Objects;

public class ExcelTimestampConvert implements Converter<Long> {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public Class supportJavaTypeKey() {
        return Long.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Long value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        ExcelProperty annotation=contentProperty.getField().getAnnotation(ExcelProperty.class);
        if(!Objects.isNull(annotation)){
            String converterName=annotation.converter().getName();
            if(converterName.equals(ExcelTimestampConvert.class.getName())){
                return new WriteCellData<>(dateFormat.format(value));
            }
        }
        return new WriteCellData<>(value.toString());
    }

}
